#!/bin/bash

#存放结果的变量
ret1=()
ret2=()
ret3=()
ret1[3]=0
ret2[3]=0
ret3[3]=0

#文件处理函数
function handle_file() {
    local filename="$1"
    local curline=1
    while read -r line || [[ -n ${line} ]]; do
        array=($(echo ${line} | tr '[:punct:]' " "))
        for i in ${array[*]}; do
            if [[ "${i}" =~ ^[a-zA-Z]+$ ]] && [[ ${#i} -gt ${ret3[3]} ]]; then
                ret3[0]=${filename}
                ret3[1]=${curline}
                ret3[2]=${i}
                ret3[3]=${#i}
                if [[ ${ret3[3]} -gt ${ret2[3]} ]]; then
                    temp=(`echo ${ret2[*]}`)
                    ret2=(`echo ${ret3[*]}`)
                    ret3=(`echo ${temp[*]}`)
                    if [[ ${ret2[3]} -gt ${ret1[3]} ]]; then
                        temp=(`echo ${ret1[*]}`)
                        ret1=(`echo ${ret2[*]}`)
                        ret2=(`echo ${temp[*]}`)
                    fi
                fi
            fi
        done
        ((curline++))
    done < $1
}


#文件判断函数
function listFile() {
    for file in `ls -a $1`; do
        if [ -d "$1/${file}" ] && [ "${file}" != "." ] && [ "${file}" != ".." ]; then
            listFile "$1/${file}"
        else
            file "$1/${file}" | grep text > /dev/null
            if [[ $? -eq 0 ]]; then
                handle_file "$1/${file}"
            fi
        fi
    done
}


#处理传入的参数
total=$#

#
while [[ total -gt 0 ]]; do
    file $1 | grep text > /dev/null
    if [[ $? -eq 0 ]]; then
        handle_file  $1
    fi
    file $1 | grep directory > /dev/null
    if [[ $? -eq 0 ]]; then
        listFile $1
    fi
    shift
    ((total--))
done

#输出结果
echo ${ret1[*]}
echo ${ret2[*]}
echo ${ret3[*]}